package com.tc.juc.design;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Auther: tianchao
 * @Date: 2021/10/4 18:54
 * @Description:
 */
@Slf4j
public class TestOrderReenTrantlock {
    static final ReentrantLock lock = new ReentrantLock();
    static final Condition t2RunCondition = lock.newCondition();
    static boolean t2Run = false;
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            lock.lock();
            try {
                while (!t2Run){
                    try {
                        t2RunCondition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.info("1");
            } finally {
                lock.unlock();
            }
        },"t1");
        Thread t2 = new Thread(()->{
            lock.lock();
            try {
                log.info("2");
                t2Run = true;
                t2RunCondition.signalAll();
            }finally {
                lock.unlock();
            }
        },"t2");
        t1.start();
        t2.start();
    }
}
